<?php
class Shop_Model_SelectCity extends Shop_Model_Base{

	/**
	 * 表示用変換
	 * @param $cityList
	 * @param $params
	 * @return 変換後リスト
	 */
	public function convertData($cityList, $params)
	{
    	$DispList = array();
    	$indexNo = 0;
        $beforePrefCd = null;
		$temp = array();
		$temp['Category'] = '3';
		
        foreach ($cityList as $cityData)
        {
            $prefCd = $cityData['TODOUFUKEN_CD'];

            // 都道府県が異なる場合
            if ($beforePrefCd != $prefCd)
            {
            	$DispList["$prefCd"]['PrefName'] = $cityData['TODOUFUKEN_NAME'];
            	
            	// tho 20141124 追加
            	// 政令指定都市
            	$DispList["$prefCd"]['Siteitosi_data'] = array();
            	
                $DispList["$prefCd"]['CityList'] = array();
                $beforeCityCd = null;
				$temp['PrefCd']   = $prefCd;
				$temp['PrefName'] = Apm_Config_Search::$TODOUFUKEN_NAME_CODE_LIST[$prefCd];
            }
            
            $prefCityCd = $prefCd . $cityData["SHIKUCHOUSON_CD"];
            
            // 静的URL取得
            $temp['CityCd'] = $prefCityCd;
            $cityData['STATIC_URL'] = Apm_Bukkenurl::generateUrlByParametersShop($temp);
            	
            if (!empty($params['CityCd']) && in_array($prefCityCd, $params['CityCd']))
            {
                $cityData['checked'] = "checked='checked'";
            }
            
            // tho 20141125 追加
            // 指定都市CDが存在する場合
            if (!empty($cityData['SITEITOSI_CD']))
            {
                $siteitosiCd = $cityData['SITEITOSI_CD'];
                // 指定都市が異なる場合
                if (array_key_exists($siteitosiCd, $DispList["$prefCd"]['Siteitosi_data']) == false)
                {
                    $DispList["$prefCd"]['Siteitosi_data']["$siteitosiCd"]['SITEITOSI_NAME']  = $cityData['SITEITOSI'];
                    $DispList["$prefCd"]['Siteitosi_data']["$siteitosiCd"]['SITEITOSI_ROMAN'] = $cityData['SITEITOSI_ROMAN'];
                    $DispList["$prefCd"]['Siteitosi_data']["$siteitosiCd"]['CityList'] = array();
                }
                // 市区データ追加
                $DispList["$prefCd"]['Siteitosi_data']["$siteitosiCd"]['CityList'][] = $cityData;
            }
            else
            {
               $DispList["$prefCd"]['CityList'][] = $cityData;
            }
            // tho 20141125 追加　End
            // tho 20141125 削除↓ 
            //$DispList["$prefCd"]['CityList'][] = $cityData;
            $beforePrefCd = $prefCd;
        }
        
        return $DispList;
	}

	/**
     * 店舗を探す（エリアから検索）sql文生成
     * @param $params
     * @param $db_params
     * @return sql文
     */
	protected function generateSql($params, &$db_params)
	{
		$strQryWhere = '';
		
		// 条件文生成
		$strSrhKbn = $params['SrhKbn'];
		$PrefCdList = array();
		
		// 都道府県コードが設定されている場合
		if (!empty($params['PrefCd']))
		{
			$PrefCdList = array_filter($params['PrefCd']);
		}
		
		// 都道府県コードが存在する場合
		if (count($PrefCdList) > 0)
		{
			$strQryWhere .= ' AND M05_TENPO_TODOUFUKEN_CD ' . $this->generateWhereIn("prefCdM05", $PrefCdList, $db_params);
		}
		// サービス内容から検索
		$strQryWhere .= $this->generateCondService($params);
		
		// ダミー店舗除外
		$strQryWhere .= $this->generateNotinDummyShop("M05_TENPO_CD", $db_params);

// SQL生成
$sql = <<< DOC_END
SELECT
	 M05.TENPO_COUNT
	,M01.M01_TODOUFUKEN_CD AS TODOUFUKEN_CD
	,M01.M01_TODOUFUKEN AS TODOUFUKEN_NAME
	,M01.M01_TODOUFUKEN_KANA AS TODOUFUKEN_KANA
	,M01.M01_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD
	,M01.M01_SHIKUCHOUSON AS SHIKUCHOUSON_NAME
	,M01.M01_SHIKUCHOUSON_KANA AS SHIKUCHOUSON_KANA
    --tho 20141124　追加
    ,M44.M44_SITEITOSI_CD AS SITEITOSI_CD
    ,M44.M44_SITEITOSI AS SITEITOSI
	,M44_SITEITOSI_ROMAN AS SITEITOSI_ROMAN
    --tho 20141124　追加 end
FROM
	(
		SELECT
			 COUNT(*) AS TENPO_COUNT
			,M05_TENPO_TODOUFUKEN_CD AS TODOUFUKEN_CD
			,M05_TENPO_SHIKUCHOUSON_CD AS SHIKUCHOUSON_CD
		FROM
			 M05TENPO_INFO
		WHERE
			M05_KOUKAI_FLAG = 1
		AND (M05_TENPO_CD > '01000000' AND M05_TENPO_CD < '48000000')
		{$strQryWhere}
		GROUP BY
			 M05_TENPO_TODOUFUKEN_CD
			,M05_TENPO_SHIKUCHOUSON_CD
	) M05
--tho 20141124 修正
--	,M01JYUUSHO M01
--WHERE
--	M05.TODOUFUKEN_CD = M01.M01_TODOUFUKEN_CD
--AND M05.SHIKUCHOUSON_CD = M01.M01_SHIKUCHOUSON_CD
	INNER JOIN M01JYUUSHO M01 ON M05.TODOUFUKEN_CD = M01.M01_TODOUFUKEN_CD AND M05.SHIKUCHOUSON_CD = M01.M01_SHIKUCHOUSON_CD
	LEFT OUTER JOIN M44SITEITOSI_SIKU M44S ON M44S.M44_TODOUFUKEN_CD = M01.M01_TODOUFUKEN_CD AND M44S.M44_SHIKUCHOUSON_CD = M05.SHIKUCHOUSON_CD
    LEFT OUTER JOIN M44SITEITOSI M44 ON M44.M44_TODOUFUKEN_CD = M44S.M44_TODOUFUKEN_CD AND M44.M44_SITEITOSI_CD = M44S.M44_SITEITOSI_CD
--tho 20141124 修正 end
GROUP BY
	 M05.TENPO_COUNT
	,M01.M01_TODOUFUKEN_CD
	,M01.M01_TODOUFUKEN
	,M01.M01_TODOUFUKEN_KANA
	,M01.M01_SHIKUCHOUSON_CD
	,M01.M01_SHIKUCHOUSON
	,M01.M01_SHIKUCHOUSON_KANA
	--tho 20141124　追加 
    ,M44.M44_SITEITOSI_CD
	,M44.M44_SITEITOSI
	,M44_SITEITOSI_ROMAN
	--tho 20141124　追加 end
ORDER BY
	 M01.M01_TODOUFUKEN_CD ASC
	,M01.M01_SHIKUCHOUSON_CD ASC
DOC_END;
		return $sql;
	}
}
?>